# Standalone build for the GliOGLvf.dll backend.
#
# Produces exactly one artifact — GliOGLvf_reimpl.dll. This is a NEW OpenGL 3.3
# core-profile backend implementing the same GLI_DRV_* ABI as the retail
# GliDX6vf.dll, which Rayman2.exe loads at runtime. There is no retail GL driver
# to match; success is visual parity with the DX6/retail backend.
#
#   cmake -S GliOGLvf -B GliOGLvf/build -A Win32
#   cmake --build GliOGLvf/build --config Release --target gliogl_reimpl
#   -> GliOGLvf/build/Release/GliOGLvf_reimpl.dll
#
# Win32 (x86) is mandatory: the EXE is 32-bit and loads a 32-bit driver.
# x87 (/arch:IA32 /fp:precise) is kept so the reused CPU clip pipeline makes the
# same keep/drop decisions as the DX6 backend (clip parity).

cmake_minimum_required(VERSION 3.15)

project(GliOGLvf_reimpl
    DESCRIPTION "OpenGL 3.3 core GLI backend for Rayman 2"
    LANGUAGES C)

# --- GLAD loader (OpenGL 3.3 core, generated with glad 0.1.36) ---------------
# Vendored in glad/; compiled into a small static lib. glad.h includes
# <KHR/khrplatform.h> and glad.c includes <glad/glad.h>, so the include root is
# glad/include (which contains glad/ and KHR/ subdirs).
add_library(glad STATIC "${CMAKE_CURRENT_SOURCE_DIR}/glad/src/glad.c")
target_include_directories(glad PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/glad/include")
if(MSVC)
    # Match the backend's static CRT (/MT) so the two objects link without a
    # MSVCRT-vs-libucrt conflict.
    set_target_properties(glad PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded")
endif()

# --- The backend ------------------------------------------------------------
# Every .c under src/ (subsystem subdirs are picked up automatically).
file(GLOB_RECURSE GLIOGLVF_SOURCES CONFIGURE_DEPENDS
    "${CMAKE_CURRENT_SOURCE_DIR}/src/*.c")

add_library(gliogl_reimpl SHARED ${GLIOGLVF_SOURCES})

target_include_directories(gliogl_reimpl PRIVATE
    "${CMAKE_CURRENT_SOURCE_DIR}/include")

# opengl32 for WGL + the GL entry points GLAD resolves; gdi32 for pixel-format /
# HDC; user32 for window/foreground calls. No DirectX of any kind.
target_link_libraries(gliogl_reimpl PRIVATE glad opengl32 gdi32 user32)

set_target_properties(gliogl_reimpl PROPERTIES
    PREFIX ""                         # no "lib" prefix
    OUTPUT_NAME "GliOGLvf_reimpl")     # -> GliOGLvf_reimpl.dll

if(MSVC)
    set_target_properties(gliogl_reimpl PROPERTIES
        MSVC_RUNTIME_LIBRARY "MultiThreaded")  # static CRT, /MT
    # Keep x87 to match the DX6 clip pipeline's keep/drop output (clip parity).
    target_compile_options(gliogl_reimpl PRIVATE /arch:IA32 /fp:precise)
    # The 41 GLI_DRV_* exports use __declspec(dllexport); __cdecl exports are
    # emitted undecorated, exactly as Rayman2.exe resolves them by GetProcAddress.
endif()
